package com.ants.boot.test.utils;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import java.net.URL;
import java.util.Arrays;

/**
 * @author : 蚂蚁会花呗
 * @date : 2023-08-30 16:49
 **/
public class DouYin {

    public static void main(String[] args) throws Exception {
//        String contents = "5.10 Pxs:/ 复制打开抖音，看看【爵士舞 可能有关】# bubblepop舞蹈挑战 # 泫雅 老胳膊老腿 浅扭一下# bubblepop舞蹈挑战# 泫雅... https://v.douyin.com/ieeHtQR9/";
//        download(contents);
        String json = "{\"name\":\"张三\",\"age\":null}";
//        JSONConfig config = new JSONConfig();
//        config.setIgnoreNullValue(true);
        cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(json);
        System.out.println(object.toString());
    }

    public static void download(String contents) {
        //换行分割的抖音分享链接
        String[] split = contents.split(StringPool.NEWLINE);
        for (String str : split){
            downloadLogic(str);
        }
    }

    public static void downloadLogic(String content) {
        // 识别抖音带段链接的字符串
        String initialUrlStr = ReUtil.get(Validator.URL, content, 0);
        String body = HttpUtil.get(initialUrlStr);
        String initialUrl = ReUtil.get(Validator.URL, body, 0);
        // 获取抖音视频长连接
        URL finalUrl = URLUtil.url(initialUrl);
        String path = finalUrl.getPath();
        String[] split = path.split(StringPool.SLASH);
        // 获取视频ID
        String videoId = ArrayUtil.get(split, split.length - 1);
        // 访问抖音API接口获取视频JSON
        String TikTokApi = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=";
        String apiJsonStr = HttpUtil.get(TikTokApi + videoId);
        JSONObject jsonObject = JSON.parseObject(apiJsonStr);
        JSONArray item_list = jsonObject.getJSONArray("item_list");
        JSONObject item = item_list.getJSONObject(0);
        JSONObject video = item.getJSONObject("video");
        // 视频播放地址
        String video_url = video.getJSONObject("play_addr").getJSONArray("url_list").getString(0);
        // 封面地址
        String origin_cover = video.getJSONObject("origin_cover").getJSONArray("url_list").getString(0);
//        String videoUrl = this.uploadVideo(video_url);
//        String coverUrl = this.uploadCover(origin_cover);
        // 保存数据库
//        TiktokVideoTblBean tiktokVideo = new TiktokVideoTblBean();
//        tiktokVideo.setVideoId(videoId);
//        tiktokVideo.setOriginalUrl(video_url);
//        tiktokVideo.setOssUrl(videoUrl);
//        tiktokVideo.setDescription(item.getString("desc"));
//        tiktokVideo.setCoverUrl(coverUrl);
//        tiktokVideoMapper.insert(tiktokVideo);
        System.out.println(video_url);
        System.out.println(origin_cover);
    }

}
